package com.mall.order.biz.handler;

import com.mall.commons.tool.exception.BizException;
import com.mall.commons.tool.utils.NumberUtils;
import com.mall.order.biz.callback.SendEmailCallback;
import com.mall.order.biz.callback.TransCallback;
import com.mall.order.biz.context.CreateOrderContext;
import com.mall.order.biz.context.TransHandlerContext;
import com.mall.order.constant.OrderRetCode;
import com.mall.order.constants.OrderConstants;
import com.mall.order.dal.entitys.Order;
import com.mall.order.dal.entitys.OrderItem;
import com.mall.order.dal.persistence.OrderItemMapper;
import com.mall.order.dal.persistence.OrderMapper;
import com.mall.order.dto.CartProductDto;
import com.mall.order.utils.GlobalIdGeneratorUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.dao.DuplicateKeyException;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.UUID;

/**
 *  ciggar
 * create-date: 2019/8/1-下午5:01
 * 初始化订单 生成订单
 */

@Slf4j
@Component
public class InitOrderHandler extends AbstractTransHandler {
    @Autowired
    private OrderMapper orderMapper;

    @Autowired
    private OrderItemMapper orderItemMapper;

    @Override
    public boolean isAsync() {
        return false;
    }

    @Override
    public boolean handle(TransHandlerContext context) {

        //向下转型
        CreateOrderContext orderContext = (CreateOrderContext) context;
        //插入订单表
        Order order = new Order();
        order.setBuyerNick(orderContext.getBuyerNickName());
        //后期改用发号器生成
        String orderId = UUID.randomUUID().toString();
        order.setOrderId(orderId);
        order.setUserId(orderContext.getUserId());
        order.setStatus(OrderConstants.ORDER_STATUS_INIT);
        order.setPayment(orderContext.getOrderTotal());
        order.setCreateTime(new Date());
        order.setUpdateTime(new Date());
        orderMapper.insert(order);
        //插入订单商品表
        List<CartProductDto> cartProductDtoList = orderContext.getCartProductDtoList();
        List<Long> buyProductIds = new ArrayList<>();

        for (CartProductDto cartProductDto : cartProductDtoList) {
            OrderItem orderItem = new OrderItem();
            String orderItemId = UUID.randomUUID().toString();
            orderItem.setId(orderItemId);
            orderItem.setItemId(cartProductDto.getProductId());
            orderItem.setNum(cartProductDto.getProductNum().intValue());
            orderItem.setPicPath(cartProductDto.getProductImg());
            orderItem.setPrice(cartProductDto.getSalePrice().doubleValue());
            orderItem.setTitle(cartProductDto.getProductName());
            BigDecimal total = cartProductDto.getSalePrice().multiply(new BigDecimal(cartProductDto.getProductNum()));
            orderItem.setTotalFee(total.doubleValue());
            orderItem.setStatus(1);
            orderItem.setOrderId(orderId);

            buyProductIds.add(cartProductDto.getProductId());
            //插入订单商品关联表中
            orderItemMapper.insert(orderItem);
        }
        //生成订单之后要给createOrderContext中的orderId赋值
        orderContext.setOrderId(orderId);
        //给createOrderContext中的buyProductIds赋值
        orderContext.setBuyProductIds(buyProductIds);
        //插入订单物流信息表

        return true;
    }
}
